package com.cndw.kungfu.model.undercity;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.domain.MonsterDeploy;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Scene;
import com.cndw.kungfu.domain.Undercity;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;

/**
 * 玩家副本对象
 * 
 * @author fantadust
 * @date 2011-4-22
 */
public class PlayerUndercity {
	private static int signNum = 0;
	/**
	 * 唯一编号
	 */
	private int id;
	/**
	 * 副本id
	 */
	private int undercityId;
	/**
	 * 进入时间
	 */
	private int enterTime;
	/**
	 * 玩家id
	 */
	private long masterId;
	/**
	 * 通天图、神兵阵过关奖励 默认0无奖励
	 */
	private int rewardId;
	/**
	 * 是否通关
	 */
	private boolean passThroug;
	/**
	 * 是否要强制弹出玩家
	 */
	private boolean dead;
	/**
	 * 已经战胜的分布列表
	 */
	private Set<Integer> overDeployIds = new HashSet<Integer>();
	/**
	 * 已经通过的关卡
	 */
	private Map<Integer, Integer> sceneMap = new HashMap<Integer, Integer>();

	private Set<Long> memberIds;
	/**
	 * 双倍爆率
	 */
	private boolean rateDouble;
	/**
	 * 战力系数
	 */
	private int powerCoef;

	public PlayerUndercity(long playerId, int undercityId) {
		this.masterId = playerId;
		this.undercityId = undercityId;
		enterTime = TimeProcesser.getUnixTime();
		initUniqueId();
	}

	public void initUniqueId() {
		synchronized (PlayerUndercity.class) {
			signNum++;
			signNum = signNum > Integer.MAX_VALUE ? 0 : signNum;
			id = signNum;
		}
	}

	public int getId() {
		return id;
	}

	public int getUndercityId() {
		return undercityId;
	}

	public int getEnterTime() {
		return enterTime;
	}


	public int getRewardId() {
		return rewardId;
	}

	public void setRewardId(int rewardId) {
		this.rewardId = rewardId;
	}

	public boolean isPassThroug() {
		return passThroug;
	}

	public void setPassThroug(boolean passThroug) {
		this.passThroug = passThroug;
	}

	public Undercity getUndercity() {
		return Context.getUndercityServer().getUndercity(undercityId);
	}

	public boolean isDead() {
		return dead;
	}

	public void setNowSceneId(int nowSceneId) {
		Scene scene = Context.getSceneServer().getSceneById(nowSceneId);
		if (scene.getUpScene() > 0) {// 不需要条件的门开启
			openSecne(scene.getUpScene());
		}
		if (scene.getDownScene() > 0) {
			openSecne(scene.getDownScene());
		}
		if (getUndercity().getType() == Undercity.typeSoldierCheckpoint) {
			// 神兵阵新场景重新开始计时
			this.enterTime = TimeProcesser.getUnixTime();
		}
	}

	public void passDeploy(int deployId) {
		overDeployIds.add(deployId);
	}

	public boolean isPassDeploy(int deployId) {
		return overDeployIds.contains(deployId);
	}

	public Set<Integer> getOverDeployIds() {
		return overDeployIds;
	}

	public void openSecne(int sceneId) {
		sceneMap.put(sceneId, 1);
	}

	public void passScene(int sceneId, int endTime) {
		sceneMap.put(sceneId, 2);
		Undercity undercity = getUndercity();
		Context.getUndercityServer().passThroug(masterId, sceneId, undercity.getId(), enterTime, endTime);
		if (undercity.getExitSceneId() != sceneId) {
			Scene scene = Context.getSceneServer().getSceneById(sceneId);
			if (scene.getRightScene() > 0) {
				
				Scene rightScene = Context.getSceneServer().getSceneById(scene.getRightScene());
				if (rightScene == null) {
					return ;
				}
				openSecne(scene.getRightScene());
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("teleport", scene.getRightTeleport());
				map.put("scene", scene.getRightScene());
				map.put("rightSceneName", rightScene.getName());
				if (memberIds == null) {
					Context.getTcpServer().broadcast(map, ConstantsRadio.sceneOpen, endTime, masterId);
				} else {
					Context.getTcpServer().broadcast(map, ConstantsRadio.sceneOpen, endTime, memberIds);
				}

				//RequestHandler.log.debug("开启"+rightScene.getName());
			}
		}
	}

	public boolean isOpenScene(int sceneId) {
		return sceneMap.containsKey(sceneId) && sceneMap.get(sceneId) > 0;
	}

	public boolean isPass(int sceneId) {
		return sceneMap.containsKey(sceneId) && sceneMap.get(sceneId) == 2;
	}

	public boolean checkPassScene(int sceneId,int endTime) {
		Scene scene = Context.getSceneServer().getSceneById(sceneId);
		int openParam1 = scene.getOpenParam1();
		int openParam2 = scene.getOpenParam2();
		switch (scene.getOpenKey()) {
		case Constants.sceneKeyCleanMonster:// 杀死全部怪物
		case Constants.sceneKeyClearOrBody:
			List<MonsterDeploy> deploys = Context.getUndercityServer().getDeploys(sceneId);
			for (MonsterDeploy e : deploys) {
				if (isPassDeploy(e.getId()) == false) {
					return false;
				}
			}
			passScene(sceneId, endTime);

			break;
		case Constants.sceneKeySpecifyMonster: // 杀死指定怪物
			List<MonsterDeploy> deploys1 = Context.getUndercityServer().getDeploys(sceneId);
			int killNum = 0;
			for (MonsterDeploy e : deploys1) {
				if (isPassDeploy(e.getId()) && e.getDefaultMonsterId() == openParam1) {
					killNum++;
				}
			}
			openParam2 = openParam2 <=0 ? 1:openParam2;
			if (killNum < openParam2) {
				return false;
			}
			passScene(sceneId, endTime);
			break;
		default:
			break;
		}

		return true;
	}

	public void setMemberIds(Set<Long> memberIds) {
		this.memberIds = memberIds;
	}

	public void broadcast(Object obj, String actionName) {
		if (memberIds != null) {
			Context.getTcpServer().broadcast(obj, actionName, memberIds);
		} else {
			Context.getTcpServer().broadcast(obj, actionName, masterId);
		}

	}

	public void checkFailed() {
		// 通天图或神兵阵玩家死亡强制退出
		if (getUndercity().getType() == Undercity.typePicCheckpoint || getUndercity().getType() == Undercity.typeSoldierCheckpoint) {
			dead = true;
		}
	}

	/**
	 * 原地复活，状态加满
	 */
	public void revive() {
		dead = false;
		Player player = Context.getPlayerServer().getByPlayerId(masterId);
		player.alterHp(player.getMaxHp());
		player.alterMp(player.getMaxMp());
		player.commitSync();
	}

	public void setMasterId(long masterId) {
		this.masterId = masterId;
	}

	public boolean isRateDouble() {
		return rateDouble;
	}

	public void setRateDouble(boolean rateDouble) {
		this.rateDouble = rateDouble;
	}

	public int getPowerCoef() {
		return powerCoef;
	}

	public void setPowerCoef(int powerCoef) {
		this.powerCoef = powerCoef;
	}
	
	
	
}
